// pages/register/register.js
const { $Message } = require('../../dist/base/index');
const { $Toast } = require('../../dist/base/index');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    username:"",
    password:"",
    tel:"",
    images:[],
    isrule:true
  },

  showimage(){
    wx.previewImage({
      urls: this.data.image,  //所有要预览的图片
    })
  },

  chooseImage(){
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success (res) {
        that.setData({
          images:res.tempFilePaths
        })        
      }
    })
  },

  userNameInput:function(e){
    this.setData({
      username:e.detail.detail.value
    })
  },
  passWordInput:function(e){
    this.setData({
      password:e.detail.detail.value
    })
  },

  telInput:function(e){
    this.setData({
      tel:e.detail.detail.value
    })
  },

  register() {
    this.data.isrule = true;

    if(this.data.tel.length != 11){
      this.data.isrule = false;
      $Message({
        content: '请输入11位的手机号码',
        type: 'error'
      });
    }
    if(this.data.password.length < 5 || this.data.password.length>12){
      this.data.isrule = false;
      $Message({
        content: '密码长度必须为5-12位',
        type: 'error'
      });
    }
    if(this.data.username.length < 5 || this.data.password.length>12){
      this.data.isrule = false;
      $Message({
        content: '注册账号长度必须为5-12位',
        type: 'error'
      });
    }

    if(this.data.isrule == true){
      //用户注册
      var app = getApp();
      var baseurl = app.globalData.BaseUrl;
      let that = this
      wx.uploadFile({
        url: baseurl+'/api/user/register', //仅为示例，非真实的接口地址
        filePath: that.data.images[0],
        name: 'image',
        formData: {
          user: that.data.username,
          pass:that.data.password,
          tel:that.data.tel
        },
        success (res){
          var code = JSON.parse(res.data).code;
          if(code == 0){
            //提示成功
            wx.showToast({
              title: '注册成功！', // 标题
              icon: 'success',  // 图标类型，默认success
              duration: 2000  // 提示窗停留时间
            })
            //跳转到首页
            setTimeout(function(){
              wx.switchTab({
              url: '../shouye/shouye',
              })
            },2000)
          }else if(res.statusCode == 500){
            $Toast({
              content: '网络异常,请重试',
              type: 'warning'
            });
          }else{
            $Toast({
              content: '账号已存在',
              type: 'warning'
            });
          }
          
        }
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})